// pages/personnal/activityDetail/activityDetail.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 一键添满
   */
  chooseGift:function(){
    let openid = app.globalData.openId
    let orderId = this.data.orderId
    let money = this.data.order.needWangNum - this.data.order.hasWangNum
    let cid = app.globalData.cid
    let getTitleDetailUrl = app.globalData.baseUrl + "pay"
    let data = {
      openid: openid,
      orderId: orderId,
      money: money,
      cid: cid
    }
    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      // console.log(res)
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: 'MD5',
        paySign: res.data.paySign,
        success: function (event) {
          let addWangUrl = app.globalData.baseUrl + "addWang"
          data = {
            orderId: orderId,
            cid: cid,
            wangNum: money,
            note: "无"
          }
          util.requestLoading(addWangUrl, data, "GET").then(res => {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            });
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/main/main',
              })
            }, 2000)
          })
        },
        fail: function (error) {
          // fail     
          // console.log("支付失败")
          // console.log(error)
          wx.showToast({
            title: '支付失败',
            icon: 'loading',
            duration: 2000
          });
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/main/main',
            })
          }, 2000)
        },
        complete: function () {
          // complete     
          // console.log("pay complete")
        }
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let status = options.status
    let orderId = options.orderId
    that.setData({
      status: status,
      orderId: orderId
    })
    let getTitleDetailUrl = app.globalData.baseUrl + "activityDetail"
    let data = {
      orderId: orderId
    }
    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      // console.log(res)
      that.setData({
        order: res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let name = this.data.order.giftName
    let orderId = this.data.orderId
    let cid = wx.getStorageSync('cid')
    // console.log(name + orderId + cid)
    return {
      title: '分享',
      path: '/pages/shareFriends/shareFriends?name=' + name + '&orderId=' + orderId + '&cid=' + cid
    }
  }
})